package com.tow.group.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tow.group.bean.User;
import com.tow.group.dao.DatabaseDao;
import com.tow.group.dao.IDatabaseDao;

public class RegisterServlet extends HttpServlet {

	/*
	 * 登陆注册功能主要涉及到的知识点是servlet，jdbc，mysql
	 * 主要思路：
	 * 					
	 * 					用户在填写注册信息提交form表单到servlet
	 * */
	int a = -1 ;
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		User user = new User();
		String userName = request.getParameter("nickname");
		String pwd = request.getParameter("password");
		String accountNumber = request.getParameter("phone");
		String mobile = accountNumber;
		
		user.setUser_Name(userName);
		user.setAccount_Number(accountNumber);
		user.setPassWord(pwd);
		user.setMobile(mobile);
		
		IDatabaseDao register = new DatabaseDao();
		try {
			a = register.register(user);
			if(a == 1){
				response.sendRedirect("/Tencent/qq_sy.jsp");
			}else{
				out.print("<h1>注册失败！！</h1>");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
